<template>
    <div class="cmt-container">
        <br>
        <h3 class="title">评论吐槽</h3>
        <br>
        <textarea placeholder="请输入要喷的内容:(120字以内)" maxlength="120" v-model="msg"></textarea>

        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <br>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comments" :key="item.avatar_url">
                <div class="cmt-title">
                    第{{i+1}}楼&nbsp;&nbsp;用户:{{item.contact_name}}&nbsp;&nbsp;发表时间:2019-4-27 17:25
                </div>

                <div class="cmt-body">
                    {{item.descr==='undefined' ? '此用户很懒,嘛都没说' : item.descr}}
                </div>
            </div>


        </div>


        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    import {
        Toast
    } from 'mint-ui'
    export default {
        data() {
            return {
                pageIndex: 1,
                comments: [],
                msg: ''
            }
        },

        created() {
            this.getComments();
        },
        methods: {
            getComments() {
                //.get('api/getComments/'+this.id+'?pageindex='+this.pageIndex)
                this.$http.get('public/images/data.json').then(result => {
                    if (result.body.status == 0) {
                        this.comments = result.body.contact;
                        console.log(this.comments);
                    } else {
                        Toast('获取评论失败');
                    }
                });
            },
            getMore() { //加载更多的评论
                this.pageIndex++;

                // this.getComments();
                for (let index = 0; index < 8; index++) {

                    this.comments.push({
                        avatar_url: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1427309730,2323684280&fm=26&gp=0.jpg' +
                            this.pageIndex + index,
                        contact_name: "袁大哥",
                        descr: "加载更多评论---" + this.pageIndex
                    })
                }
                //加载新评论,要拼接上旧的评论
                //this.comments = this.comments.concat(result.body.contact);
            },
            postComment() {
                //校验数据
                if (this.msg.trim().length === 0) {
                    return Toast('评论内容不能为空')
                }

                // this.$http.post('api/postcomment' + this.$route.params.id, {
                //     comments: this.msg.trim()
                // }).then(function (result) {
                //     if (result.body.status===0) {
                //         var cmt={
                //             user_name:'匿名用户',
                //             add_time:Date().now(),
                //             content:this.msg.trim()
                //         }
                //         this.comments.unshift(cmt);
                //         this.msg='';
                //     }else{
                //          Toast('评论发表失败')
                //     }
                // })

                var cmt = {
                    avatar_url: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1427309730,2323684280&fm=26&gp=0.jpg',
                    contact_name: '匿名用户',
                    descr: this.msg.trim()
                }
                this.comments.unshift(cmt);
                this.msg='';
            }
        },
        props: ["id"]
    }
</script>

<style lang="less">
    .cmt-container {

        textarea {
            font-size: 14px;
            height: 80px;
            margin: 0;
        }

        .cmt-list {
            .cmt-item {
                margin: 5px 0;
                font-size: 13px;

                .cmt-title {
                    line-height: 30px;
                    background-color: #ccc;
                }

                .cmt-body {
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }

    }
</style>